package com.cjkj.applet.controller;

import com.cjkj.applet.domain.dto.EquipmentDto;
import com.cjkj.applet.service.AppletAccountService;
import com.cjkj.applet.service.IAppletBindInfoService;
import com.cjkj.common.core.domain.R;
import com.cjkj.common.core.web.domain.AjaxResult;
import com.cjkj.common.security.annotation.InnerAuth;
import com.cjkj.common.security.annotation.RepeatSubmit;
import com.cjkj.common.security.service.AppletTokenService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * 小程序端账号绑定设备情况
 */
@RestController
@RequestMapping("/bind-info")
@Validated
public class AppletBindInfoController {
    @Resource
    private IAppletBindInfoService appletBindInfoService;

    @Resource
    private AppletAccountService appletAccountService;

    @Resource
    private AppletTokenService appletTokenService;

    /**
     * 绑定设备
     * @param equipment 设备信息
     * @return
     */
    @PostMapping("add")
    @RepeatSubmit
    public AjaxResult add(@RequestBody @Valid EquipmentDto equipment){
        appletBindInfoService.add(equipment);
        return AjaxResult.success();
    }


    /**
     * 解绑
     * @param equipmentNos 设备编号
     * @return
     */
    @PostMapping("unbind")
    @RepeatSubmit
    public AjaxResult unbind(@RequestBody @Size(min = 1, message = "设备编号不能为空！")  List<String> equipmentNos){
        appletBindInfoService.unbind(equipmentNos);
        return AjaxResult.success();
    }

    /**
     * 获取绑定的设备
     * @return
     */
    @GetMapping("list")
    public AjaxResult getList(String dataType){
        return AjaxResult.success(appletBindInfoService.getList(dataType));
    }

    /**
     * 设备移交
     * @param equipmentNos
     * @return
     */
    @PostMapping("/receive")
    @RepeatSubmit
    public AjaxResult receive(@RequestBody List<String> equipmentNos){
        return appletBindInfoService.receive(equipmentNos);
    }

    /**
     * 解绑医生账号
     * @return
     */
    @DeleteMapping("unbindDoctor")
    public AjaxResult unbindDoctor(HttpServletRequest request){
        int i = appletAccountService.unbindDoctor();
        if (i > 0) {
            appletTokenService.setUserDoctorAccountCache(request, "");
            return AjaxResult.success();
        }
        return AjaxResult.error("解绑医生账号出错！");
    }

    /**
     * 绑定医生账号
     * @param account 账号
     * @param password 密码
     */
    @GetMapping("bindDoctor")
    public AjaxResult bindDoctor(HttpServletRequest request,
                                 @RequestParam("account") @NotBlank(message = "医生账号不能为空！") String account,
                                 @RequestParam("password") @NotBlank(message = "医生密码不能为空！") String password){
        int i = appletAccountService.bindDoctor(account, password);
        if (i > 0) {
            appletTokenService.setUserDoctorAccountCache(request, account);
            return AjaxResult.success();
        }
        return AjaxResult.error("绑定医生账号出错！");
    }

    /**
     * 获取医生签名
     * @return
     */
    @GetMapping("getDoctorSign")
    public AjaxResult getSign(@RequestParam("doctor") String doctor){
        return AjaxResult.success("success", appletAccountService.getDoctorSign(doctor));
    }

    /**
     * 清除绑定的医生账号信息
     * @param doctorAccount 医生账号
     */
    @InnerAuth
    @GetMapping("/clearDoctorInfo")
    public R<?> clearDoctorInfo(@RequestParam("doctor") String doctorAccount){
        appletAccountService.clearUserDoctorInfo(doctorAccount);
        return R.ok();
    }
}
